<?php

namespace app\models\searchs;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\models\Park;

/**
 * ParkSearch represents the model behind the search form about `app\models\Park`.
 */
class ParkSearch extends Park
{
    public $cname;
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'build_date', 'start_date', 'pricipal_id', 'company_id', 'contact_id'], 'integer'],
            [['code', 'name', 'addr', 'scale', 'introduction', 'tel', 'email', 'comment', 'map_image', 'qualification_image','cname'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = Park::find();
        $query->joinWith(['company']);
        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);
        $dataProvider->setSort([
            'attributes' => [
                'name',
                'code',
                'addr',
                /* 其它字段不要动 */
                /*  下面这段是加入的 */
                /*=============*/
                'cname' => [
                    'asc' => ['shbb_company.name' => SORT_ASC],
                    'desc' => ['shbb_company.name' => SORT_DESC],
                    'label' => '所属公司'
                ],
                /*=============*/
            ]
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        $query->andFilterWhere([
            'id' => $this->id,
            'build_date' => $this->build_date,
            'start_date' => $this->start_date,
            'pricipal_id' => $this->pricipal_id,
            'company_id' => $this->company_id,
            'contact_id' => $this->contact_id,
        ]);

        $query->andFilterWhere(['like', 'shbb_park.code', $this->code])
             ->andFilterWhere(['like', 'shbb_park.name', $this->name])
            ->andFilterWhere(['like', 'shbb_park.addr', $this->addr])
            ->andFilterWhere(['like', 'shbb_park.scale', $this->scale])
            ->andFilterWhere(['like', 'shbb_park.introduction', $this->introduction])
            ->andFilterWhere(['like', 'shbb_park.tel', $this->tel])
            ->andFilterWhere(['like', 'shbb_park.email', $this->email])
            ->andFilterWhere(['like', 'shbb_park.comment', $this->comment])
            ->andFilterWhere(['like', 'shbb_park.map_image', $this->map_image])
            ->andFilterWhere(['like', 'shbb_park.qualification_image', $this->qualification_image])
            ->andFilterWhere(['like', 'shbb_company.name', $this->cname]);

        return $dataProvider;
    }
}
